<script setup>
import {onMounted, reactive, ref, withDirectives} from "vue";
import {deleteById, page} from "@/apis/sys/user.js";
import {OPERATE_DRAWER_MODE} from "@/common/constant.js";
import permission from "@/directive/permission.js";
import {useMessage, useDialog, NSpace, NButton} from "naive-ui";
import OperateDrawer from "@/views/sys/user/operate-drawer.vue";

const message = useMessage()
const dialog = useDialog()

onMounted(async () => {
  await loadTableData()
})

const loading = ref(false)
const tableData = ref([])
const pagination = reactive({
  itemCount: 0,
  page: 1,
  pageSize: 10,
  pageSizes: [10, 20, 30, 40, 50],
  showSizePicker: true,
  showQuickJumper: true,
  prefix: () => `共 ${pagination.itemCount} 条`,
  onUpdatePage: async (page) => {
    pagination.page = page
    await loadTableData()
  },
  onUpdatePageSize: async (pageSize) => {
    pagination.pageSize = pageSize
    await loadTableData()
  }
})
const columns = ref([
  {
    title: '用户名',
    key: 'username',
  },
  {
    title: '部门编号',
    key: 'deptNo',
  },
  {
    title: '部门名称',
    align: 'center',
    key: 'deptName',
  },
  {
    title: '创建时间',
    align: 'center',
    key: 'createdAt',
  },
  {
    title: '最后更新时间',
    align: 'center',
    key: 'updatedAt',
  },
  {
    title: '操作',
    align: 'center',
    key: 'operate',
    render: (row) => {
      return h(NSpace, null, () => {
        return [
          withDirectives(h(NButton, {onClick: () => showModifyDrawer(row.id),}, () => '编辑'), [[permission, 'sys:user:modify']]),
          withDirectives(h(NButton, {onClick: () => showDetailDrawer(row.id),}, () => '详情'), [[permission, 'sys:user:detail']]),
          withDirectives(h(NButton, {onClick: () => onDelete(row.id),}, () => '删除'), [[permission, 'sys:user:delete']]),
        ]
      })
    }
  }
])

const refreshPage = async () => {
  await loadTableData()
}

const loadTableData = async () => {
  loading.value = true
  const {code, data} = await page(pagination.page, pagination.pageSize)
  loading.value = false
  if (code === 0) {
    tableData.value = data.records
    pagination.itemCount = Number.parseInt(data.total)
  }
}
const operateDrawerOption = reactive({
  visible: false,
  mode: OPERATE_DRAWER_MODE.DETAIL,
  id: undefined
})

const showAddDrawer = () => {
  operateDrawerOption.mode = OPERATE_DRAWER_MODE.ADD
  operateDrawerOption.visible = true
}

const showModifyDrawer = (id) => {
  operateDrawerOption.mode = OPERATE_DRAWER_MODE.EDIT
  operateDrawerOption.id = id
  operateDrawerOption.visible = true
}

const showDetailDrawer = (id) => {
  operateDrawerOption.mode = OPERATE_DRAWER_MODE.DETAIL
  operateDrawerOption.id = id
  operateDrawerOption.visible = true
}

const onDelete = async (id) => {
  dialog.warning({
    title: '删除',
    content: '确定删除吗？',
    positiveText: '确定',
    negativeText: '取消',
    onPositiveClick: async () => {
      const {code} = await deleteById(id)
      if (code === 0) {
        message.success("删除成功")
        await loadTableData()
      }
    }
  })
}
</script>

<template>
  <n-space vertical>
    <n-button type="primary" @click="showAddDrawer">新增</n-button>
    <n-data-table
        remote
        ref="table"
        :columns="columns"
        :data="tableData"
        :loading="loading"
        :pagination="pagination"
        :row-key="(rowData) => rowData.id"
    />
  </n-space>
  <operate-drawer
      v-model:show="operateDrawerOption.visible"
      :mode="operateDrawerOption.mode"
      :id="operateDrawerOption.id"
      @success="refreshPage"/>
</template>

<style scoped>

</style>